package com.masterclient.app.modules.aftersale.adapter;

import android.graphics.Typeface;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eoner.baselibrary.bean.aftersale.AfterSaleListBean;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.Utils;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.masterclient.app.R;

import java.util.List;

/**
 * 作者：sean
 * 创建时间：2017/8/26 10:46
 * 功能说明：
 */

public class AfterSaleListAdapter extends BaseQuickAdapter<AfterSaleListBean.DataBean.ShItemsBean, BaseViewHolder> {


    public AfterSaleListAdapter() {
        super(R.layout.item_aftersale_order);
    }

    @Override
    protected void convert(BaseViewHolder helper, AfterSaleListBean.DataBean.ShItemsBean item) {

//        helper.setText(R.id.tv_order_no, "售后单号：" + item.getSh_refund_no());
        helper.setText(R.id.tv_status, item.getSh_status_label());
        LinearLayout ll_shop = helper.getView(R.id.ll_shop);

        ll_shop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                ARouter.getInstance().build(AroutePath.STORE_ACTIVITY)
//                        .withString("is_dealer", item.getSh_is_dealer()?"1":"0")
//                        .withString("is_agent", item.getSh_is_agent()?"1":"0")
//                        .withString("store_id", item.getSh_seller().getSh_idX()).navigation();
            }
        });

        helper.getView(R.id.tv_contact_seller).setVisibility(View.GONE);
        helper.setGone(R.id.tv_detail, "1".equals(item.getSh_button().getSh_view_btn()))
                .setGone(R.id.tv_cancel, "1".equals(item.getSh_button().getSh_cancal_btn()));

        TextView cross_border_flag_view = helper.getView(R.id.cross_border_flag_view);
        if ("2".equals(item.getSh_seller().getSh_seller_type())) {
            cross_border_flag_view.setVisibility(View.VISIBLE);
        } else {
            cross_border_flag_view.setVisibility(View.GONE);
        }

        TextView tv_shop_tag = helper.getView(R.id.tv_shop_tag);
        tv_shop_tag.setTypeface(Typeface.createFromAsset(mContext.getAssets(), "fonts/iconfont.ttf"));
        TextView tv_shop_name = helper.getView(R.id.tv_shop_name);
        if (item.getSh_is_dealer() && item.getSh_dealer_info() != null &&
            !TextUtils.isEmpty(item.getSh_dealer_info().getSh_dealer_name())){
            //经销商品
            tv_shop_name.setText(item.getSh_dealer_info().getSh_dealer_name());
        }else {
            tv_shop_name.setText(item.getSh_seller().getSh_store_name());
        }

        ((ImageView) helper.getView(R.id.tv_return)).setImageResource("2".equals(item.getSh_refund_type())
                ? R.mipmap.icon_bufa : "1".equals(item.getSh_refund_type()) ? R.mipmap.tuihuo1_shouhou : R.mipmap.tuikuan1_shouhou);
        helper.setText(R.id.tv_return_type, item.getSh_refund_type_label());
//        if("1".equals(item.getSh_refund_type())){//退货退款
//            helper.setText(R.id.tv_return_type,"退货退款");
//            ivReturn.setText(R.string.tuihuotuikuan);
//            ivReturn.setTextColor(0xffcc363c);
//        }else if("0".equals(item.getSh_refund_type())){
//            ivReturn.setText(R.string.tuikuan);
//            ivReturn.setTextColor(0xffee8a31);
//        }
        LinearLayout llGood = helper.getView(R.id.ll_good);
        llGood.removeAllViews();
        if (item.getSh_products() != null && item.getSh_products().size() > 0) {
            for (int i = 0; i < item.getSh_products().size(); i++) {

                AfterSaleListBean.DataBean.ShItemsBean.ShProductBean bean = item.getSh_products().get(i);
                View view = View.inflate(mContext, R.layout.item_aftersale_goods, null);
                ViewTransformUtil.glideImageView(mContext, bean.getSh_image(),(ImageView) view.findViewById(R.id.iv_good), R.mipmap.defaults,
                        ScreenUtils.dp2px(107), ScreenUtils.dp2px(107));
//                GlideUtil.displayImage(mContext,bean.getSh_image(),(ImageView)view.findViewById(R.id.iv_good),R.mipmap.defaults,90,90);
//            App.picasso.load(bean.getSh_image()).resize(ScreenUtils.dp2px(90),ScreenUtils.dp2px(90))
//                    .into((ImageView)view.findViewById(R.id.iv_good));

                ((TextView) view.findViewById(R.id.tv_title)).setText(bean.getSh_name());
                String skus = "";
                List<AfterSaleListBean.DataBean.ShItemsBean.ShProductBean.ShPropertyBean> sh_property = bean.getSh_property();
                if (sh_property != null) {
                    for (AfterSaleListBean.DataBean.ShItemsBean.ShProductBean.ShPropertyBean propertyBean : sh_property) {
                        skus += ("“" + propertyBean.getSh_alias_name() + "”");
                    }
                }
                ((TextView) view.findViewById(R.id.tv_skus)).setText("规格：" + skus);
                ((TextView) view.findViewById(R.id.tv_gos_num)).setText("x" + bean.getSh_qty_ordered());

                TextView returnMoney = ((TextView) view.findViewById(R.id.tv_return_money));
                LinearLayout returnMoneyLl = view.findViewById(R.id.ll_return_money);
                TextView returnIntegral = ((TextView) view.findViewById(R.id.tv_return_integral));
                LinearLayout returnIntegralLl = view.findViewById(R.id.ll_return_integral);
                Utils.setDin(returnMoney, mContext);
                Utils.setDin(returnIntegral, mContext);
                view.findViewById(R.id.view_line).setVisibility(i == item.getSh_products().size() - 1 ? View.GONE : View.VISIBLE);

                if ("2".equals(item.getSh_refund_type())) {
                    returnIntegralLl.setVisibility(View.GONE);
                    returnMoneyLl.setVisibility(View.GONE);
                } else {
                    returnMoneyLl.setVisibility(!TextUtils.isEmpty(bean.getSh_amount()) && Float.parseFloat(bean.getSh_amount()) > 0 ? View.VISIBLE : View.GONE);
                    returnIntegralLl.setVisibility(!TextUtils.isEmpty(bean.getSh_integral()) && Long.parseLong(bean.getSh_integral()) > 0 ? View.VISIBLE : View.GONE);
                    returnIntegral.setText(!TextUtils.isEmpty(bean.getSh_integral()) ? bean.getSh_integral() : "0");
                    Utils.set_price_style2(returnMoney, !TextUtils.isEmpty(bean.getSh_amount()) ? ("¥ " + bean.getSh_amount()) : "0.00");
                }
//                view.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        Intent intent = new Intent(mContext,AfterSaleDetailActivity.class);
//                        Bundle bundle = new Bundle();
//                        bundle.putString("product_id",bean.getSh_id());
//                        intent.putExtras(bundle);
//                        mContext.startActivity(intent);
//                    }
//                });


                //赠品列表

//                List<PromotionProductMessage> productMessageList = bean.getSh_promotion();
                View cross_border_layout = view.findViewById(R.id.cross_border_layout);
                cross_border_layout.setVisibility(View.GONE);
//                if(productMessageList != null && productMessageList.size() > 0){
//                    cross_border_layout.setVisibility(View.VISIBLE);
//                    LinearLayout ll_shop_present_container = (LinearLayout) view.findViewById(R.id.ll_shop_present_container);
//                    ll_shop_present_container.removeAllViews();
//                    for(PromotionProductMessage promotionProductMessage : productMessageList){
//                        View view_present_goods = View.inflate(mContext,R.layout.item_present,null);
//                        ImageView ivPresentGood = (ImageView) view_present_goods.findViewById(R.id.iv_good);
//                        TextView ivPresentTitle = (TextView) view_present_goods.findViewById(R.id.tv_title);
//                        TextView ivPresentSkus = (TextView) view_present_goods.findViewById(R.id.tv_skus);
//                        TextView ivPresentPrice = (TextView) view_present_goods.findViewById(R.id.tv_price);
//                        TextView ivPresentNum = (TextView) view_present_goods.findViewById(R.id.tv_num);
//
//                        ivPresentTitle.setText(promotionProductMessage.getSh_name());
//                        ivPresentNum.setText("×" + promotionProductMessage.getSh_qty());
//                        ivPresentSkus.setText("规格：" + promotionProductMessage.getSh_property_desc());
//                        //GlideUtil.displayRoundImage(mContext,productsBean.getSh_image(),imageView,4,R.mipmap.defaults,107,107);
//                        ll_shop_present_container.addView(view_present_goods);
//                    }
//                }else{
//                    cross_border_layout.setVisibility(View.GONE);
//                }


                llGood.addView(view);
//            helper.addOnClickListener(R.id.rl_goods);

            }


        }

        helper.addOnClickListener(R.id.tv_detail);
        helper.addOnClickListener(R.id.tv_cancel);
        helper.addOnClickListener(R.id.tv_contact_seller);


    }

}
